using System;
using System.Collections.Generic;

using Csla;

using EntModApp.EntServices.Business;

using DemoApp.DataAccess;

namespace DemoApp.BusinessLayer 
{
  public partial class CslaUserInfo : 
    ReadOnlyBase<CslaUserInfo>,
    IUser
  {
    #region [ Business Properties ]

    public static PropertyInfo<Guid> UserIdPropertyInfo = RegisterProperty(new PropertyInfo<Guid>("UserId"));
    public Guid UserId 
    { 
      get
      {
        return ReadProperty(UserIdPropertyInfo);
      }
      private set
      {
        LoadProperty(UserIdPropertyInfo, value);
      }
    }

    public static PropertyInfo<string> UserNamePropertyInfo = RegisterProperty(new PropertyInfo<string>("UserName"));
    public string UserName 
    { 
      get
      {
        return ReadProperty(UserNamePropertyInfo);
      }
      private set
      {
        LoadProperty(UserNamePropertyInfo, value);
      }
    }

    public static PropertyInfo<string> PasswordPropertyInfo = RegisterProperty(new PropertyInfo<string>("Password"));
    public string Password 
    { 
      get
      {
        return ReadProperty(PasswordPropertyInfo);
      }
      private set
      {
        LoadProperty(PasswordPropertyInfo, value);
      }
    }

    public static PropertyInfo<CslaRoleInfoCollection> RolesPropertyInfo = RegisterProperty(new PropertyInfo<CslaRoleInfoCollection>("Roles"));
    public CslaRoleInfoCollection Roles 
    { 
      get
      {
        return ReadProperty(RolesPropertyInfo);
      }
      private set
      {
        LoadProperty(RolesPropertyInfo, value);
      }
    }

    public static PropertyInfo<CslaGroupInfoCollection> GroupsPropertyInfo = RegisterProperty(new PropertyInfo<CslaGroupInfoCollection>("Groups"));
    public CslaGroupInfoCollection Groups 
    { 
      get
      {
        return ReadProperty(GroupsPropertyInfo);
      }
      private set
      {
        LoadProperty(GroupsPropertyInfo, value);
      }
    }

    public static PropertyInfo<CslaSubjectInfoCollection> CreatedSubjectsPropertyInfo = RegisterProperty(new PropertyInfo<CslaSubjectInfoCollection>("CreatedSubjects"));
    public CslaSubjectInfoCollection CreatedSubjects 
    { 
      get
      {
        return ReadProperty(CreatedSubjectsPropertyInfo);
      }
      private set
      {
        LoadProperty(CreatedSubjectsPropertyInfo, value);
      }
    }

    public static PropertyInfo<CslaSubjectInfoCollection> EditedSubjectsPropertyInfo = RegisterProperty(new PropertyInfo<CslaSubjectInfoCollection>("EditedSubjects"));
    public CslaSubjectInfoCollection EditedSubjects 
    { 
      get
      {
        return ReadProperty(EditedSubjectsPropertyInfo);
      }
      private set
      {
        LoadProperty(EditedSubjectsPropertyInfo, value);
      }
    }

    #endregion [ Business Properties ]

    #region [ Factory Methods ]
    protected internal static CslaUserInfo GetUserInfo(User data, UserLoadSettings loadSettings)
    {
      if (data == null) throw new ArgumentNullException("data");
      if (loadSettings == null) throw new ArgumentNullException("loadSettings");
      CslaUserInfo result;
      if (data is Person)
        result = CslaPersonInfo.GetPersonInfo((Person)data, PersonLoadSettings.FromUserLoadSettings(loadSettings));
      else
      if (data is Group)
        result = CslaGroupInfo.GetGroupInfo((Group)data, GroupLoadSettings.FromUserLoadSettings(loadSettings));
      else
      {
        result = new CslaUserInfo();
        result.FetchUser(data, loadSettings);
      }
      return result;
    }

    private static int ForceInitField = 0;
    protected CslaUserInfo() 
    {
      ForceInitField = 1;
    }
    #endregion [ Factory Methods ]

    #region [ Data Access ]
    protected void FetchUser(User data, UserLoadSettings loadSettings)
    {
      FetchUserData(data, loadSettings);
      FetchUserChildren(data, loadSettings);
    }

    protected void FetchUserData(User data, UserLoadSettings loadSettings)
    {
      this.UserId = data.UserId;
      this.UserName = data.UserName;
      this.Password = data.Password;
    }

    protected void FetchUserChildren(User data, UserLoadSettings loadSettings)
    {
      if (loadSettings.Inclusion.IncludeRoles)
        this.Roles = CslaRoleInfoCollection.GetRoleInfoCollection(data.Roles, loadSettings.Inclusion.IncludedRolesSettings);
      if (loadSettings.Inclusion.IncludeGroups)
        this.Groups = CslaGroupInfoCollection.GetGroupInfoCollection(data.Groups, loadSettings.Inclusion.IncludedGroupsSettings);
      if (loadSettings.Inclusion.IncludeCreatedSubjects)
        this.CreatedSubjects = CslaSubjectInfoCollection.GetSubjectInfoCollection(data.CreatedSubjects, loadSettings.Inclusion.IncludedCreatedSubjectsSettings);
      if (loadSettings.Inclusion.IncludeEditedSubjects)
        this.EditedSubjects = CslaSubjectInfoCollection.GetSubjectInfoCollection(data.EditedSubjects, loadSettings.Inclusion.IncludedEditedSubjectsSettings);
    }
    #endregion [ Data Access ]

    #region [ Explicit IUser implementation ]

    Guid IUser.UserId
    {
      get { return this.UserId; }
    }

    string IUser.UserName
    {
      get { return this.UserName; }
      set
      {
        throw new NotSupportedException();
      }
    }

    string IUser.Password
    {
      get { return this.Password; }
      set
      {
        throw new NotSupportedException();
      }
    }

    IBusinessObjectCollection<IRole> IUser.Roles
    { 
      get
      {
        return this.Roles;
      }
    }

    IBusinessObjectCollection<IGroup> IUser.Groups
    { 
      get
      {
        return this.Groups;
      }
    }

    IBusinessObjectCollection<ISubject> IUser.CreatedSubjects
    { 
      get
      {
        return this.CreatedSubjects;
      }
    }

    IBusinessObjectCollection<ISubject> IUser.EditedSubjects
    { 
      get
      {
        return this.EditedSubjects;
      }
    }


    bool IBusinessObject.IsReadonly
    {
      get { return true; }
    }

    bool IBusinessObject.IsDirty
    {
      get { return false; }
    }

    #endregion [ Explicit IUser implementation ]
  }
}
